<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;
use think\Db;

class Wallet extends Api
{
    public function tx()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            if(!isset($post['user_id']) || !isset($post['money']))
            {
                return fh(1,'error','参数不完整',[]);
            }
            $list = model('wallet')->where('user_id','=',$post['user_id'])
                ->order('create_time','desc')->find()->toArray();
            if($list['balance']<$post['money']){
                return fh(1,'error','余额不足',[]);
            }
            $balance =$list['balance']*1-$post['money']*1;
            model('wallet')->save(['user_id'=>$post['user_id'],'status'=>2,'class'=>1,'money'=>$post['money'],'balance'=>$balance]);
            return fh(200,'success','请求成功',$list);
        }else{
            return fh(1,'error','不是post请求',[]);
        }
    }
    public function ye()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            $post = array_filter($post);
            if(!isset($post['user_id'])){
                return fh(404,'error','缺少用户ID',[]);
            }
            $list = model($this->request->controller())->where('user_id','=',$post['user_id'])->select();
            // pr($list);
            if($list){
                $list = $list->toArray();
                $moeny = 0;
                foreach ($list as $k=>$v){
                    if($v['status'] == 1){
                        $moeny = $moeny*1 + $v['money']*1;
                    }elseif ($v['status'] == 2){
                        $moeny = $moeny*1 - $v['money']*1;
                    }
                }
                $list = $moeny;
            }else{
                $list = 0;
            }
            return fh(200,'success','请求成功',$list);
        }
    }
    public function index()
    {
    	if($this->request->isPost())
    	{
    		$post = post();
    		if(!isset($post['type'])) return fh(200,'error','缺少类型参数',[]);
    		if(!isset($post['user_id'])) return fh(200,'error','缺少用户数据',[]);
    		$table = $this->creattable();
			$user = model('user')->where('id','=',$post['user_id'])->find();
			if(!empty($user))
			{
				$user = $user->toArray();
			}else{
				return fh(200,'error','查询不到用户',[]);
			}
			
    		if($post['type'] == 1 && $user['fwgl_id'] > 0){
    			//服务中心数据
    			//服务中心所得
    			$hl = model('wallet')->where([['user_id','=',$post['user_id']],['class','=',4]])->select();
				$moneyd = 0;
    			if(!empty($hl)){
    				$hl = $hl->toArray();
    				foreach($hl as $v){
    					$moneyd += $v['money']*1;
    				}
    			}
    			//服务中心提现
    			$tx = model('wallet')->where([['user_id','=',$post['user_id']],['class','=',9]])->select();
				$moneyt = 0;
    			if(!empty($tx)){
    				$tx = $tx->toArray();
    				foreach($tx as $v){
    					$moneyt += $v['money']*1;
    				}
    			}
    			//余额
    			$list['yue'] = $moneyd - $moneyt;
    			
    			//产品兑换积分1
    			$cpdhjf = 0;
    			//公司证券积分2
    			$gszjjf = 0;
    			//贷款额度积分3
    			$dkedjf = 0;
    			//服务中心明细积分余额
    			$fwzxjf = model('integral')->where('fwgl_id','=',$user['fwgl_id'])->find();
    			if(!empty($fwzxjf)){
    				$fwzxjf = $fwzxjf->toArray();
    				$jfmx = model('integraldetailed')->where([['integral_id','=',$fwzxjf['id']],['user_id','=',$user['id']]])->select();
	    			if(!empty($jfmx)){
	    				$jfmx = $jfmx->toArray();
	    				foreach ($jfmx as $v){
	    					if($v['status'] == 1 && $v['class'] == 1){
	    						$cpdhjf += $v['num']*1;
	    					}elseif($v['status'] == 1 && $v['class'] == 2){
	    						$cpdhjf -= $v['num']*1;
	    					}elseif($v['status'] == 2 && $v['class'] == 1){
	    						$gszjjf += $v['num']*1;
	    					}elseif($v['status'] == 2 && $v['class'] == 2){
	    						$gszjjf -= $v['num']*1;
	    					}elseif($v['status'] == 3 && $v['class'] == 1){
	    						$dkedjf += $v['num']*1;
	    					}elseif($v['status'] == 3 && $v['class'] == 2){
	    						$dkedjf -= $v['num']*1;
	    					}
	    				}
	    			}
    			}
    			$list['cpdh'] = $cpdhjf;
    			$list['gszj'] = $gszjjf;
    			$list['dked'] = $dkedjf;
    			
    			
    			//这个服务中心下的所有商家
    			$fwzx = model('business')->where('fwgl_id','=',$user['fwgl_id'])->select();
    			if(!empty($fwzx)){
    				$fwzx = $fwzx->toArray();
    				$busidall = [];
    				foreach ($fwzx as $k=>$v){
    					if(!in_array($v['id'],$busidall)){
    						$busidall[] = $v['id'];
    					}
    				}
    				$busidall = implode(',',$busidall);
    				//所有下属商家的订单
    				$orderall = model('battery_order')->with('business.pid,user')->where([['business_id','in',$busidall],['status','=',2]])->select();
    				if(!empty($orderall)){
    					$orderall = $orderall->toArray();
	                    $all = [];
	                    foreach($orderall as $k=>$v){
		                    //来源id
	                    	$all[$k]['pid'] = $v['id'];
	                    //status  这条数据所属的类型1=》服务中心租赁订单所得，2服务中心权益积分所得，3管理中心订单所得，4管理中心权益所得
	                    	$all[$k]['status'] = 1;
	                    //user_id  租赁用户id
	                    	$all[$k]['user_id'] = $v['user_id']; 
	                    //user_id  租赁用户名称
	                    	$all[$k]['username'] = $v['user']['name']; 
	                    //zlsnanme  租赁商名称
	                    	$all[$k]['zlsnanme'] = $v['business']['pid']['name'];
	                    //ordermoney  订单金额全款
	                    	$all[$k]['ordermoney'] = $v['money'];
	                    //zlsmoney  租赁商所得
	                    	$all[$k]['zlsmoney'] = floor($v['money']*0.55*0.3*100)/100;
	                    //orderid  订单编号
	                    	$all[$k]['orderid'] = $v['orderid'];
	                    //order_status  订单金额是添加还是减少
	                    	$all[$k]['order_status'] = 1;
	                    //order_create_time  订单金额是添加还是减少
	                    	$all[$k]['order_create_time'] = date('Y-m-d H:i:s',$v['create_time']);
	                    	$all[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
	                    }
    					Db::table('bs_zjb')->insertAll($all);
    				}
    				//服务中心所有的所有积分
    				//服务中心只有权益积分
    				$jfall = model('integral')->with('integraldetailed')->where([['fwgl_id','=',$user['fwgl_id']],['source','=',7]])->select();
    				if(!empty($jfall)){
    					$jfall = $jfall->toArray();
    					$all = [];
    					foreach ($jfall as $k=>$v){
    						//积分权益
		                    //fwgl_id  服务中心id
		                    //glzx_id  管理中心id
		                    //integral  积分总数
		                    //integral_status 积分类型
		                    //integral_class 添加还是减少
		                    //integral_id 积分id 通过积分id拿下面的明细
		                    if(empty($v['integraldetailed'])){
		                    	$json = '';
		                    }else{
		                    	$json = json_encode($v['integraldetailed']);
		                    }
		                    //来源id
	                    	$all[$k]['pid'] = $v['id'];
		                    //status  这条数据所属的类型1=》服务中心租赁订单所得，2服务中心权益积分所得，3管理中心订单所得，4管理中心权益所得
		                    $all[$k]['status'] = 2;
		                    //fwgl_id  服务中心id
	                    	$all[$k]['fwgl_id'] = $user['fwgl_id'];
		                    //glzx_id  管理中心id
	                    	$all[$k]['glzx_id'] = 0;
		                    //integral  积分总数
	                    	$all[$k]['integral'] = $v['num'];
		                    //integral_status  积分类型
	                    	$all[$k]['integral_status'] = $v['source'];
		                    //integral_class  添加还是减少
	                    	$all[$k]['integral_class'] = $v['status'];
		                    //integral_id  积分id 通过积分id拿下面的明细
	                    	$all[$k]['integral_id'] = $v['id'];
		                    //integral_id  积分id 通过积分id拿下面的明细
	                    	$all[$k]['integral_mx'] = $json;
	                    	$all[$k]['create_time'] = $v['create_time'];
		                    
    					}
    					Db::table('bs_zjb')->insertAll($all);	
					}
    			}
    		}elseif($post['type'] == 2 && $user['glzx_id'] > 0){
    			//管理中心数据
    			//管理中心所得
    			$hl = model('wallet')->where([['user_id','=',$post['user_id']],['class','=',5]])->select();
				$moneyd = 0;
    			if(!empty($hl)){
    				$hl = $hl->toArray();
    				foreach($hl as $v){
    					$moneyd += $v['money']*1;
    				}
    			}
    			//管理中心提现
    			$tx = model('wallet')->where([['user_id','=',$post['user_id']],['class','=',10]])->select();
				$moneyt = 0;
    			if(!empty($tx)){
    				$tx = $tx->toArray();
    				foreach($tx as $v){
    					$moneyt += $v['money']*1;
    				}
    			}
    			//余额
    			$list['yue'] = $moneyd - $moneyt;
    			
    			
    			
    			//产品兑换积分1
    			$cpdhjf = 0;
    			//公司证券积分2
    			$gszjjf = 0;
    			//贷款额度积分3
    			$dkedjf = 0;
    			//管理中心明细积分余额
    			$fwzxjf = model('integral')->where('glzx_id','=',$user['glzx_id'])->find();
    			if(!empty($fwzxjf)){
    				$fwzxjf = $fwzxjf->toArray();
    				$jfmx = model('integraldetailed')->where([['integral_id','=',$fwzxjf['id']],['user_id','=',$user['id']]])->select();
	    			if(!empty($jfmx)){
	    				$jfmx = $jfmx->toArray();
	    				foreach ($jfmx as $v){
	    					if($v['status'] == 1 && $v['class'] == 1){
	    						$cpdhjf += $v['num']*1;
	    					}elseif($v['status'] == 1 && $v['class'] == 2){
	    						$cpdhjf -= $v['num']*1;
	    					}elseif($v['status'] == 2 && $v['class'] == 1){
	    						$gszjjf += $v['num']*1;
	    					}elseif($v['status'] == 2 && $v['class'] == 2){
	    						$gszjjf -= $v['num']*1;
	    					}elseif($v['status'] == 3 && $v['class'] == 1){
	    						$dkedjf += $v['num']*1;
	    					}elseif($v['status'] == 3 && $v['class'] == 2){
	    						$dkedjf -= $v['num']*1;
	    					}
	    				}
	    			}
    			}
    			$list['cpdh'] = $cpdhjf;
    			$list['gszj'] = $gszjjf;
    			$list['dked'] = $dkedjf;
    			
    			
    			
    			//这个管理中心下的所有商家
    			$fwzx = model('business')->where('glzx_id','=',$user['glzx_id'])->select();
    			if(!empty($fwzx)){
    				$fwzx = $fwzx->toArray();
    				$busidall = [];
    				foreach ($fwzx as $k=>$v){
    					if(!in_array($v['id'],$busidall)){
    						$busidall[] = $v['id'];
    					}
    				}
    				$busidall = implode(',',$busidall);
    				//所有下属商家的订单
    				$orderall = model('battery_order')->with('business.pid,user')->where([['business_id','in',$busidall],['status','=',2]])->select();
    				if(!empty($orderall)){
    					$orderall = $orderall->toArray();
	                    $all = [];
	                    foreach($orderall as $k=>$v){
		                    //来源id
	                    	$all[$k]['pid'] = $v['id'];
	                    //status  这条数据所属的类型1=》服务中心租赁订单所得，2服务中心权益积分所得，3管理中心订单所得，4管理中心权益所得
	                    	$all[$k]['status'] = 3;
	                    //user_id  租赁用户id
	                    	$all[$k]['user_id'] = $v['user_id']; 
	                    //user_id  租赁用户名称
	                    	$all[$k]['username'] = $v['user']['name']; 
	                    //zlsnanme  租赁商名称
	                    	$all[$k]['zlsnanme'] = $v['business']['pid']['name'];
	                    //ordermoney  订单金额全款
	                    	$all[$k]['ordermoney'] = $v['money'];
	                    //zlsmoney  租赁商所得
	                    	$all[$k]['zlsmoney'] = floor($v['money']*0.55*0.1*100)/100;
	                    //orderid  订单编号
	                    	$all[$k]['orderid'] = $v['orderid'];
	                    //order_status  订单金额是添加还是减少
	                    	$all[$k]['order_status'] = 1;
	                    //order_create_time  订单金额是添加还是减少
	                    	$all[$k]['order_create_time'] = date('Y-m-d H:i:s',$v['create_time']);
	                    	$all[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
	                    }
    					Db::table('bs_zjb')->insertAll($all);
    				}
    				//管理中心所有的所有积分
    				//管理中心只有权益积分
    				$jfall = model('integral')->with('integraldetailed')->where([['fwgl_id','=',$user['fwgl_id']],['source','=',17]])->select();
    				if(!empty($jfall)){
    					$jfall = $jfall->toArray();
    					$all = [];
    					foreach ($jfall as $k=>$v){
    						//积分权益
		                    //fwgl_id  服务中心id
		                    //glzx_id  管理中心id
		                    //integral  积分总数
		                    //integral_status 积分类型
		                    //integral_class 添加还是减少
		                    //integral_id 积分id 通过积分id拿下面的明细
		                    if(empty($v['integraldetailed'])){
		                    	$json = '';
		                    }else{
		                    	$json = json_encode($v['integraldetailed']);
		                    }
		                    //来源id
	                    	$all[$k]['pid'] = $v['id'];
		                    //status  这条数据所属的类型1=》服务中心租赁订单所得，2服务中心权益积分所得，3管理中心订单所得，4管理中心权益所得
		                    $all[$k]['status'] = 4;
		                    //fwgl_id  服务中心id
	                    	$all[$k]['fwgl_id'] = 0;
		                    //glzx_id  管理中心id
	                    	$all[$k]['glzx_id'] = $user['glzx_id'];
		                    //integral  积分总数
	                    	$all[$k]['integral'] = $v['num'];
		                    //integral_status  积分类型
	                    	$all[$k]['integral_status'] = $v['source'];
		                    //integral_class  添加还是减少
	                    	$all[$k]['integral_class'] = $v['status'];
		                    //integral_id  积分id 
	                    	$all[$k]['integral_id'] = $v['id'];
		                    //integral_id  积分id 通过积分id拿下面的明细
	                    	$all[$k]['integral_mx'] = $json;
	                    	$all[$k]['create_time'] = $v['create_time'];
    					}
    					Db::table('bs_zjb')->insertAll($all);	
					}
    			}
    		}elseif($post['type'] == 3 && $user['canpinsf'] != 99){
    			//代理商数据
    			//代理商所得
    			$hl = model('wallet')->where([['user_id','=',$post['user_id']],['class','=',2]])->select();
				$moneyd = 0;
    			if(!empty($hl)){
    				$hl = $hl->toArray();
    				foreach($hl as $v){
    					$moneyd += $v['money']*1;
    				}
    			}
    			//代理商提现
    			$tx = model('wallet')->where([['user_id','=',$post['user_id']],['class','=',11]])->select();
				$moneyt = 0;
    			if(!empty($tx)){
    				$tx = $tx->toArray();
    				foreach($tx as $v){
    					$moneyt += $v['money']*1;
    				}
    			}
    			//余额
    			$list['yue'] = $moneyd - $moneyt;
    			
    			
    			//产品兑换积分1
    			$cpdhjf = 0;
    			//公司证券积分2
    			$gszjjf = 0;
    			//贷款额度积分3
    			$dkedjf = 0;
    			$fwzxid = [];
    			//代理商明细积分余额
    			$fwzxjf = model('integral')->where([
    				['user_id','=',$user['id']],
    				['source','in','1,2,3,4,5']
    				])->select();
    			if(!empty($fwzxjf)){
    				$fwzxjf = $fwzxjf->toArray();
    				foreach($fwzxjf as $v){
    					if(!in_array($v['id'],$fwzxid)){
	    					$fwzxid[] = $v['id'];
	    				}
	    			}
	    			$fwzxid = implode(',',$fwzxid);
					//来源(1=>产品代理积分,2=>兑换产品,3=>兑换贷款额度,4=>产品代理平级积分,5=>产品代理互助积分,6=>商家权益积分,7=>服务中心权益积分,8=>租赁代理积分,9=>租赁代理平级积分,10=>租赁代理互助积分,11=>服务中心租赁积分,12=>直推商家代理,13=>直推合伙人,14=>下级商户租赁代理业绩分成,15=>会员权益积分,16=>代理权益积分,17=>管理中心权益积分) 
	    			
	    			$jfmx = model('integraldetailed')->where([['integral_id','in',$fwzxid],['user_id','=',$user['id']]])->select();
	    			if(!empty($jfmx)){
	    				$jfmx = $jfmx->toArray();
	    				foreach ($jfmx as $v){
	    					if($v['status'] == 1 && $v['class'] == 1){
	    						$cpdhjf += $v['num']*1;
	    					}elseif($v['status'] == 1 && $v['class'] == 2){
	    						$cpdhjf -= $v['num']*1;
	    					}elseif($v['status'] == 2 && $v['class'] == 1){
	    						$gszjjf += $v['num']*1;
	    					}elseif($v['status'] == 2 && $v['class'] == 2){
	    						$gszjjf -= $v['num']*1;
	    					}elseif($v['status'] == 3 && $v['class'] == 1){
	    						$dkedjf += $v['num']*1;
	    					}elseif($v['status'] == 3 && $v['class'] == 2){
	    						$dkedjf -= $v['num']*1;
	    					}
	    				}
	    			}
    				
    				
    			}
    			
    			$list['cpdh'] = $cpdhjf;
    			$list['gszj'] = $gszjjf;
    			$list['dked'] = $dkedjf;
    			
    			
    			$jfall = model('integral')->where([['user_id','=',$user['id']],['source','=',1]])->select();
    			if(!empty($jfall)){
    					$jfall = $jfall->toArray();
    					$all = [];
    					foreach ($jfall as $k=>$v){
    						//积分权益
		                    //fwgl_id  服务中心id
		                    //glzx_id  管理中心id
		                    //integral  积分总数
		                    //integral_status 积分类型
		                    //integral_class 添加还是减少
		                    //integral_id 积分id 通过积分id拿下面的明细
		                    if(empty($v['integraldetailed'])){
		                    	$json = '';
		                    }else{
		                    	$json = json_encode($v['integraldetailed']);
		                    }
		                    //来源id
	                    	$all[$k]['pid'] = $v['id'];
		                    //status  这条数据所属的类型1=》服务中心租赁订单所得，2服务中心权益积分所得，3管理中心订单所得，4管理中心权益所得，5代理商各种积分
		                    $all[$k]['status'] = 5;
		                    //fwgl_id  服务中心id
	                    	$all[$k]['fwgl_id'] = 0;
		                    //glzx_id  管理中心id
	                    	$all[$k]['glzx_id'] = 0;
		                    //integral  积分总数
	                    	$all[$k]['integral'] = $v['num'];
		                    //integral_status  积分类型
	                    	$all[$k]['integral_status'] = $v['source'];
		                    //integral_class  添加还是减少
	                    	$all[$k]['integral_class'] = $v['status'];
		                    //integral_id  积分id 通过积分id拿下面的明细
	                    	$all[$k]['integral_id'] = $v['id'];
		                    //integral_id  积分id 通过积分id拿下面的明细
	                    	$all[$k]['integral_mx'] = $json;
	                    	$all[$k]['create_time'] = $v['create_time'];
    					}
    					Db::table('bs_zjb')->insertAll($all);	
					}
    		}elseif($post['type'] == 4 && $user['business_id'] > 0){
    			//商家数据
    			//商家所得
    			$hl = model('wallet')->where([['user_id','=',$post['user_id']],['class','=',7]])->select();
				$moneyd = 0;
    			if(!empty($hl)){
    				$hl = $hl->toArray();
    				foreach($hl as $v){
    					$moneyd += $v['money']*1;
    				}
    			}
    			//商家提现
    			$tx = model('wallet')->where([['user_id','=',$post['user_id']],['class','=',12]])->select();
				$moneyt = 0;
    			if(!empty($tx)){
    				$tx = $tx->toArray();
    				foreach($tx as $v){
    					$moneyt += $v['money']*1;
    				}
    			}
    			//余额
    			$list['yue'] = $moneyd - $moneyt;
    			
    			$orderall = model('battery_order')->with('business.pid,user')->where([['business_id','=',$user['business_id']],['status','=',2]])->select();
				if(!empty($orderall)){
					$orderall = $orderall->toArray();
                    $all = [];
                    foreach($orderall as $k=>$v){
	                    //来源id
                    	$all[$k]['pid'] = $v['id'];
                    //status  这条数据所属的类型1=》服务中心租赁订单所得，2服务中心权益积分所得，3管理中心订单所得，4管理中心权益所得，5代理商各种积分，6商人租赁订单所得
                    	$all[$k]['status'] = 6;
                    //user_id  租赁用户id
                    	$all[$k]['user_id'] = $v['user_id']; 
                    //user_id  租赁用户名称
                    	$all[$k]['username'] = $v['user']['name']; 
                    //zlsnanme  商家名称
                    	$all[$k]['zlsnanme'] = $v['business']['name'];
                    //ordermoney  订单金额全款
                    	$all[$k]['ordermoney'] = $v['money'];
                    //zlsmoney  商家所得
                    	$all[$k]['zlsmoney'] = floor($v['money']*0.1*100)/100;
                    //orderid  订单编号
                    	$all[$k]['orderid'] = $v['orderid'];
                    //order_status  订单金额是添加还是减少
                    	$all[$k]['order_status'] = 1;
                    //order_create_time  订单金额是添加还是减少
                    	$all[$k]['order_create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                    	$all[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                    }
					Db::table('bs_zjb')->insertAll($all);
				}
    		}elseif($post['type'] == 5 && $user['zls'] > 0){
    			//承租人数据
    			//承租人所得
    			$hl = model('wallet')->where([['user_id','=',$post['user_id']],['class','=',6]])->select();
    			// pr($hl);
				$moneyd = 0;
    			if(!empty($hl)){
    				$hl = $hl->toArray();
    				foreach($hl as $v){
    					$moneyd += $v['money']*1;
    				}
    			}
    			//承租人提现
    			$tx = model('wallet')->where([['user_id','=',$post['user_id']],['class','=',12]])->select();
				$moneyt = 0;
    			if(!empty($tx)){
    				$tx = $tx->toArray();
    				foreach($tx as $v){
    					$moneyt += $v['money']*1;
    				}
    			}
    			//余额
    			$list['yue'] = $moneyd - $moneyt;
    			//产品兑换积分1
    			$cpdhjf = 0;
    			//公司证券积分2
    			$gszjjf = 0;
    			//贷款额度积分3
    			$dkedjf = 0;
				//承租人明细积分余额
    			$fwzxjf = model('integral')->where([['user_id','=',$user['id']],['source','=',18]])->find();
    			if(!empty($fwzxjf)){
    				$fwzxjf = $fwzxjf->toArray();
	    			$jfmx = model('integraldetailed')->where([['integral_id','=',$fwzxjf['id']],['user_id','=',$user['id']]])->select();
	    			if(!empty($jfmx)){
	    				$jfmx = $jfmx->toArray();
	    				foreach ($jfmx as $v){
	    					if($v['status'] == 1 && $v['class'] == 1){
	    						$cpdhjf += $v['num']*1;
	    					}elseif($v['status'] == 1 && $v['class'] == 2){
	    						$cpdhjf -= $v['num']*1;
	    					}elseif($v['status'] == 2 && $v['class'] == 1){
	    						$gszjjf += $v['num']*1;
	    					}elseif($v['status'] == 2 && $v['class'] == 2){
	    						$gszjjf -= $v['num']*1;
	    					}elseif($v['status'] == 3 && $v['class'] == 1){
	    						$dkedjf += $v['num']*1;
	    					}elseif($v['status'] == 3 && $v['class'] == 2){
	    						$dkedjf -= $v['num']*1;
	    					}
	    				}
	    			}	
    			}
    			
    			$list['cpdh'] = $cpdhjf;
    			$list['gszj'] = $gszjjf;
    			$list['dked'] = $dkedjf;
    			$orderall = model('battery_order')->with('business.pid,user')->where([['business_id','in',$user['business_id']],['status','=',2]])->select();
    			
				if(!empty($orderall)){
					$orderall = $orderall->toArray();
                    $all = [];
                    // pr($orderall);
                    foreach($orderall as $k=>$v){
                    //status  这条数据所属的类型1=》服务中心租赁订单所得，2服务中心权益积分所得，3管理中心订单所得，4管理中心权益所得，5代理商各种积分，6商人租赁订单所得，7承租人租赁订单所得
                    //来源id
                    	$all[$k]['pid'] = $v['id'];
                    	$all[$k]['status'] = 7;
                    //user_id  租赁用户id
                    	$all[$k]['user_id'] = $v['user_id']; 
                    //user_id  租赁用户名称
                    	$all[$k]['username'] = $v['user']['name']; 
                    //zlsnanme  商家名称
                    	$all[$k]['zlsnanme'] = $v['business']['name'];
                    //ordermoney  订单金额全款
                    	$all[$k]['ordermoney'] = $v['money'];
                    //zlsmoney  商家所得
                    	$all[$k]['zlsmoney'] = floor($v['money']*0.3*100)/100;
                    //orderid  订单编号
                    	$all[$k]['orderid'] = $v['orderid'];
                    //order_status  订单金额是添加还是减少
                    	$all[$k]['order_status'] = 1;
                    //order_create_time  订单金额是添加还是减少
                    	$all[$k]['order_create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                    	$all[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                    }
					Db::table('bs_zjb')->insertAll($all);
				}
    		}else{
    			return fh(200,'error','该类型用户没有对应的身份',[]);
    		}
			$list['data'] = model('zjb')->order('create_time desc')->paginate(10)->toArray();
			return fh(200,'success','',$list);
    	}
    }
    public function creattable()
	{
	    $sql = "create temporary table `bs_zjb` (
	                    `id` int(11) NOT NULL primary key AUTO_INCREMENT,
	                    `pid` varchar(11),
	                    `status` varchar(30),
	                    `user_id` int(11),
	                    `username` varchar(125),
	                    `zlsnanme` varchar(125),
	                    `ordermoney` varchar(20),
	                    `zlsmoney` varchar(11),
	                    `orderid` varchar (30),
	                    `order_status` varchar (30),
	                    `order_create_time` varchar (30),
	                    `fwgl_id` int(11),
	                    `glzx_id` int(11),
	                    `integral` int(11),
	                    `integral_status` int(9),
	                    `integral_class` int(9),
	                    `integral_id` int(9),
	                    `create_time` varchar(30),
	                    `integral_mx` text)";
	                    //status  这条数据所属的类型1=》租赁订单所得，2权益积分所得
	                    //user_id  租赁用户id
	                    //zlsnanme  租赁商名称
	                    //ordermoney  订单金额全款
	                    //zlsmoney  租赁商所得
	                    //orderid  订单编号
	                    //order_status  订单金额是添加还是减少
	                    //order_create_time  订单生产时间-开始租赁时间
	                    //积分权益
	                    //fwgl_id  服务中心id
	                    //glzx_id  管理中心id
	                    //integral  积分总数
	                    //integral_status 积分类型
	                    //integral_class 添加还是减少
	                    //integral_id 积分id 通过积分id拿下面的明细
	                    //create_time 数据添加时间
	    $affected = Db::execute($sql);
	    return $affected;
	}
    
    public function indexs()
    {
    	if($this->request->isPost())
    	{
	    	$post = post();
            $limit = $post['limit'] ?? 10;
	    	$list = model($this->request->controller())->where('user_id','=',$post['user_id'])->paginate($limit);
	    	if(!empty($list)){
	    		$list = $list->toArray();
	    		foreach($list['data'] as $k=>$v){
	    			if($v['status'] == 1){
	    				$list['data'][$k]['status'] = 0;
	    			}elseif($v['status'] == 2){
	    				$list['data'][$k]['status'] = 1;
	    			}
	    		}
	    	}
	    	return fh(200,'success','成功',$list);
    	}
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
